/*******************************************************************************
 * This file is part of Crunch Graphics.
 *
 * Crunch Graphics is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * Crunch Graphics is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with
 * Crunch Graphics.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

package com.crunch.graphics.gpu;
import javax.media.opengl.GL;

/** Encapsulates the state of the viewport on the GPU.
 */
public class GpuViewportState extends GpuObject {
	/** Sets the state of the GPU to the state represented by this object.
	 */
	public void bind() {
		getGpuState().bind(this);
	}

	/** Sets the viewport position.
	 */
	public void setViewportPosition(int x, int y) {
		viewportX = x;
		viewportY = y;
	}

	/** Sets the viewport size.
	 */
	public void setViewportSize(int w, int h) {
		viewportWidth = w;
		viewportHeight = h;
	}

	/** Sets the viewport position and size.
	 */
	public void setViewport(int x, int y, int w, int h) {
		viewportX = x;
		viewportY = y;
		viewportWidth = w;
		viewportHeight = h;
	}

	/** Returns the viewport x.
	 */
	public int getViewportX() {
		return viewportX;
	}

	/** Returns the viewport y.
	 */
	public int getViewportY() {
		return viewportY;
	}

	/** Returns the viewport position.
	 */
	public int[] getViewportPosition() {
		return new int[] { viewportX, viewportY };
	}

	/** Returns the viewport width.
	 */
	public int getViewportWidth() {
		return viewportWidth;
	}

	/** Returns the viewport height.
	 */
	public int getViewportHeight() {
		return viewportHeight;
	}

	/** Returns the viewport size.
	 */
	public int[] getViewportSize() {
		return new int[] { viewportWidth, viewportHeight };
	}

	GpuViewportState(GpuState state) {
		super(state);
		viewportX = 0;
		viewportY = 0;
		viewportWidth = 0;
		viewportHeight = 0;
	}

	/** Copy constructor.
	 */
	public GpuViewportState(GpuViewportState other) {
		super(other.getGpuState());
		viewportX = other.getViewportX();
		viewportY = other.getViewportY();
		viewportWidth = other.getViewportWidth();
		viewportHeight = other.getViewportHeight();
	}

	private int viewportX, viewportY;           // viewport position
	private int viewportWidth, viewportHeight;  // viewport size

	// compares changes against current state and apply differences
	void apply(GpuViewportState currentState) {
		if (viewportX != currentState.viewportX || viewportY != currentState.viewportY ||
				viewportWidth != currentState.viewportWidth || viewportHeight != currentState.viewportHeight) {
			getGpuState().getGl().glViewport(viewportX, viewportY, viewportWidth, viewportHeight);
			currentState.viewportX = viewportX;
			currentState.viewportY = viewportY;
			currentState.viewportWidth = viewportWidth;
			currentState.viewportHeight = viewportHeight;
		}
	}

	// sets values to the current OpenGL state
	void getCurrentState() {
		int[] viewportResult = new int[4];
		getGpuState().getGl().glGetIntegerv(GL.GL_VIEWPORT, viewportResult, 0);
		viewportX = viewportResult[0];
		viewportY = viewportResult[1];
		viewportWidth = viewportResult[2];
		viewportHeight = viewportResult[3];
	}
}